import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ost_flutter_module/class/entity/store/store_address_entity.dart';
import 'package:ost_flutter_module/class/store/address_page/store_address_edit_page.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';
import 'package:ost_flutter_module/common/styles.dart';
import 'package:ost_flutter_module/common/utils.dart';
import 'package:ost_flutter_module/help/ost_route.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_bottom_button.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_radius_container.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_size_icon_button.dart';
import 'package:ost_flutter_module/widget/custom_widget/sliver_footer.dart';
import 'package:ost_flutter_module/widget/loading/loading_empty_widget.dart';
import 'package:ost_flutter_module/widget/loading/loading_error_widget.dart';
import 'package:ost_flutter_module/widget/loading/loading_view.dart';
import 'package:ost_flutter_module/widget/loading/loading_widget.dart';

import 'bloc/address_page_bloc.dart';

class StoreAddressPageBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocListener<AddressPageBloc, AddressPageState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      child: BlocBuilder<AddressPageBloc, AddressPageState>(
          builder: (context, state) {
        var _dataModel = context.read<AddressPageBloc>().dataModel;
        var _list = _dataModel.dataList;

        return LoadingView(
          status: _dataModel?.loadingStatus,
          loadingContent: LoadingWidget(),
          errorContent: LoadingErrorWidget(
            errorTitle: _dataModel?.errorMsg,
            onRetry: () => context.read<AddressPageBloc>().add(
                  AddressPageFetchData(
                      // isRefresh: true,
                      action: null),
                ),
          ),
          successContent: GBottomButton(
            children: [
              ListView.builder(
                itemCount: _list.length,
                itemBuilder: (BuildContext context, int index) {
                  StoreAddressEntity entity = _list[index];

                  // // 底部增加一个空item占位
                  // if (index == _list.length - 1) {
                  //   return SliverFooter();
                  // }

                  return StoreAddressPageBodyCard(
                    entity: entity,
                    onPressed: (type) {
                      if (type == 0)
                        Navigator.pop(context);
                      else
                        GMaterialPagePush.materialPagePush(
                          context,
                          StoreAddressEditPage(
                            type: EditAddress.edit,
                            entity: entity,
                          ),
                        );
                    },
                  );
                },
              ),
            ],
            title: '新增地址',
            onPressed: () {
              GMaterialPagePush.materialPagePush(
                  context, StoreAddressEditPage(type: EditAddress.add));
            },
          ),
          emptyContent: LoadingEmptyWidget(),
        );
      }),
    );
  }
}

class StoreAddressPageBodyCard extends StatelessWidget {
  final int row;
  final bool check;
  final StoreAddressEntity entity;
  final ValueChanged<dynamic> onPressed; // 0: 选中，1: 编辑 (EditAddress)

  const StoreAddressPageBodyCard(
      {Key key, this.row, this.check, this.onPressed, this.entity})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GContainer(
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: Adapt.px(10)),
        child: Row(
          children: [
            GIconButton(
              svg: entity.isDefault != null && entity.isDefault == 1
                  ? Utils.svgAssets('icon_selected')
                  : Utils.svgAssets('icon_unselected'),
              onPressed: () => onPressed(0),
            ),
            SizedBox(width: Adapt.px(10)),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Text('${entity.receiver}', style: TextStyles.titleStyle),
                      SizedBox(width: Adapt.px(8)),
                      Text('${entity.phone}', style: TextStyles.titleStyle),
                    ],
                  ),
                  SizedBox(height: Adapt.px(8)),
                  Text(
                    '${entity.addressProvince}${entity.addressCity}${entity.addressDistrict}${entity.addressDetail}',
                    style: TextStyles.detailTitleStyle,
                    maxLines: 2,
                  ),
                ],
              ),
            ),
            GIconButton(
              svg: Utils.svgAssets('icon_edit'),
              onPressed: () => onPressed(EditAddress.edit),
            ),
          ],
        ),
      ),
      onTap: () => onPressed(0),
    );
  }
}
